$(function() {
    initArtCateList();

    function initArtCateList() {
        $.ajax({
            type: 'GET',
            url: '/my/article/cates',
            success: function(res) {
                if (res.status !== 0) {
                    return layui.layer.msg(res.message);
                }

                layui.layer.msg(res.message);
                const data = res.data;
                // console.log(data);
                //获得渲染好的字符串
                let htmlStr = template('tpl-table', {
                    data: data
                });
                $('tbody').html(htmlStr);
            },
            timeout: 3000,
            error: function() {
                return layui.layer.msg('出错啦');
            }
        });
    }

    let indexAdd = null; //增加弹出层的页面

    $('#btnAddCate').on('click', function() {
        indexAdd = layui.layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '添加文章分类',
            content: $('#dialog-add').html()
        });
    });

    //通过代理的方式 为form-add绑定submit事件 事件委派
    $('body').on('submit', '#form-add', function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "/my/article/addcates",
            data: $(this).serialize(),
            success: function(res) {
                if (res.status !== 0) {
                    return layui.layer.msg(res.message);
                }
                layui.layer.msg(res.message);
                //根据索引关闭弹出层
                layui.layer.close(indexAdd);
                initArtCateList();
            },
            timeout: 3000,
            error: function() {
                return layui.layer.msg('出错啦');
            }
        });
    });

    let indexEdit = null; //删除弹出层的页面

    $('tbody').on('click', '.btn-edit', function() {
        //弹出一个修改文章分类的层
        indexEdit = layui.layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '修改文章分类',
            content: $('#dialog-edit').html()
        });
        let id = $(this).attr('data-id');
        //发起请求，获取数据
        $.ajax({
            type: 'GET',
            url: `/my/article/cates/${id}`,
            success: function(res) {
                if (res.status !== 0) {
                    return layui.layer.msg(res.message);
                }
                $('#_id').val(res.data._id);
                layui.form.val('form-edit', res.data);
            },
            timeout: 3000,
            error: function() {
                return layui.layer.msg('出错啦');
            }
        });
    });


    //发起修改文章分类请求
    $('body').on('submit', '#form-edit', function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize(),
            success: function(res) {
                if (res.status !== 0) {
                    return layui.layer.msg(res.message);
                }
                layui.layer.msg(res.message);
                //根据索引关闭弹出层
                layui.layer.close(indexEdit);
                initArtCateList();
            },
            timeout: 3000,
            error: function() {
                return layui.layer.msg('出错啦');
            }
        });
    });

    //删除文章分类
    $('tbody').on('click', '.btn-delete', function() {
        let id = $(this).attr('data-id');
        layui.layer.confirm('确认删除？', {
            icon: 3,
            title: '提示'
        }, function(index) {
            //发送删除请求
            $.ajax({
                type: 'GET',
                url: `/my/article/deletecate/${id}`,
                success: function(res) {
                    if (res.status !== 0) {
                        return layui.layer.msg(res.message);
                    }
                    layui.layer.msg(res.message);
                    layui.layer.close(index);
                    initArtCateList();
                },
                timeout: 3000,
                error: function() {
                    return layui.layer.msg('出错啦');
                }
            });
        });
    });
});